<?php
/*
	A standard Veneer module
	Use ajaxmodule if you need ajax
*/
abstract class Veneer_Controller_My extends Zend_Controller_Action
{
//	Required Variables

    protected $_dbArray =array();
    protected $_moduleName = null;
    protected $_module_id = 0;
    protected $_config_module;
    protected $_primary_table;

	protected $_veneer_parms;	// array of parms
	protected $_veneer_log;	// array of parms
	protected $_site_config;
	protected $_msite_config;
	protected $_defaultAction = "view";	// array of parms
	protected $_auth;	// login
	protected $_dbAdapter;	// login
	protected $_registry;	// login
	protected $_debug;	// login
	protected $_session;	// login

	function init()  // Must be after initial variables are defined.
	{
		// Load variables from registry
		$this->_registry = Zend_Registry::getInstance();
		$this->_msite_config = $this->_registry->get('msite_config');
		$this->_site_config = $this->_registry->get('site_config');
		$this->_dbAdapter = $this->_registry->get('dbAdapter');
		$this->_veneer_log = $this->_registry->get('veneerLog');
		$this->_debug = $this->_registry->get('veneer_debug');
		$this->_session = new Veneer_Session();

		// load tables

		Veneer_Controller_Helper::loadModels($this->_dbArray);
		// load parameters
		$this->_veneer_parms = $this->_getAllParams();
		$this->_registry->set('veneer_parms', $this->_veneer_parms);

		// set Paths
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$this->view->moduleUrl = $this->view->baseUrl . '/'.$this->_moduleName;

		// load module configuration
		//****set registry too for module name
		if (file_exists(SITE_PATH . 'moduleconfig/' . $this->_moduleName . '.ini')) {
			$this->view->module_config = new Zend_Config_Ini(SITE_PATH . 'moduleconfig/' . $this->_moduleName . '.ini', 'main');
		} else {
			$this->_redirect('/admin/'.$this->_moduleName.'/config/');
		}

		$this->view->addScriptPath(APP_PATH . 'Veneer/View/scripts/');
		$this->view->addHelperPath(APP_PATH. 'Veneer/View/Helper', 'Veneer_View_Helper');

		// Logged in
		$this->_auth = Zend_Auth::getInstance();

		if($this->_auth->hasIdentity())
			$this->view->loggedIn = true;
		else
			$this->view->loggedIn = false;
	}

 	function preDispatch()
	{
		// authorization
		// security
	}
    public function tagAction()
    {
		// generic function to handle tags
    }

    public function __call($method, $args)
    {
		 if ('Action' == substr($method, -6)) {
            // If the action method was not found
			if(!file_exists(APP_PATH . 'modules/'.$this->_moduleName.'/views/scripts/index/'.substr($method, 0,-6).'.phtml')) {
				if(substr($method, 0,-6) == 'index') {
			 		$this->_redirect('/');
				} else {
					$path = '/'.$this->_moduleName;
			 		$this->_redirect($path);
				}
			}
        }
    }

/*
	ListHelper - use a standard view
	read pages of data
*/
    protected function listHelper($title, $table_name, $config)
    {
	   $this->view->title = $title;
	   $this->view->config_rows = $config;

        $table = new $table_name();
        $this->view->objects = $table->fetchPage();
		$this->_helper->viewRenderer->renderScript('veneerList.phtml');
    }

    public function addHelper($title, $table_name="", $config=array())
    {
		// specify, validators, conditionals, and filters.
		//
	   $this->view->title = $title;
	   $this->view->config_rows = $config;
        $table = new $table_name();
		$table->page_size =2;
        $this->view->objects = $table->fetchPage();
		$this->_helper->viewRenderer->renderScript('veneerList.phtml');
/*
		static  $formArrayTemplate = array(
			'fieldName'=>array('validatorName1'=>null,
						'validatorName2'=>array('parms')),
			'fieldName1'=>array('validatorName1'=>null,
						'validatorName2'=>array('parm1','parm2')),
			'fieldName2'=>array('validatorName1'=>null,
						'validatorName2'=>array('parm1','parm2',
									'message'=>'custom error message'))
		);

		$form = new Zend_Form($formArray);
        if ($this->getRequest()->isPost()) {
			$form->setDataSource($_POST);
			$form->addField('artist');
			$form->addField('title');
		    if ($form->isValid()) {
		        $album = new Album();
	        	$data = $form->getData();
    			$album->insert($data);

        	    $this->_redirect('/album');
        	    return;
            }
        }

        // set up an "empty" album



		$this->_helper->viewRenderer->renderScript('veneerAdd.phtml');
*/	}
    public function deleteHelper()
    {
		/*
	set the view
*/
    }
    public function editHelper()
    {
		/*
	set the view
*/
    }

	public function postDispatch()
	{
		//$cache=new Veneer_Cache();
		$this->_registry->set('postdispatch', $this->getResponse()->getBody());
//        $this->getResponse()->appendBody("<p>*********postDispatch() called</p>\n");
	}
}

